package com.tian.web.admin.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;



import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.oscache.util.StringUtil;
import com.tian.core.action.JqGridBaseAction;
import com.tian.core.bean.Condition;
import com.tian.core.bean.ConditionOperate;
import com.tian.core.bean.ConditionType;
import com.tian.core.bean.Pages;
import com.tian.core.entity.Depart;
import com.tian.core.entity.Group;
import com.tian.core.entity.Master;
import com.tian.core.entity.Mastergroup;
import com.tian.core.entity.Ztree;
import com.tian.web.admin.service.DepartService;
import com.tian.web.admin.service.GroupService;
import com.tian.web.admin.service.MasterGroupService;
import com.tian.web.admin.service.MasterService;

@ParentPackage("json-default")//加载json 
@Namespace("/admin")
@Results( {  
	//@Result(type = "json", name = "success",params={"includeProperties","^gridModel/[/d+/]/./w+,rows, page, total, record","noCache","true","ignoreHierarchy","false"}),
	@Result(type = "dispatcher",name = "user", location = "/pages/admin/user_list.jsp"),
	@Result(type = "json")
})
public class UserAction extends JqGridBaseAction{

	private static final long serialVersionUID = 5908089272132035487L;
	@Resource
	private MasterService masterService;
	@Resource
	private MasterGroupService mgService;
	@Resource
	private GroupService groupService;
	@Resource 
	private DepartService departService;

	// 和jqGrid组件相关的参数属性
	private List<Master> gridModel;
	private List<Group> groupList;
	private List<Depart> departList;
	
	private Master master;
	private Integer groupid;
	private Integer departid;
	
	@Override
	public String execute() throws Exception {
		return "user";
	}
	
	public String list() throws Exception {
		return this.refreshGridModel();
	}
	
	public String delete() throws Exception {
		if(StringUtil.isEmpty(this.id)){
    		return ERROR;
    	}
		//删除与用户组关联
		Mastergroup mg = mgService.get("master.id", Integer.parseInt(id));
		mgService.delete(mg);
		masterService.delete(Integer.parseInt(id));
		return ajax("ajax_ok");
	}
	
	public String addOrUpdate() throws Exception {
    	if(id!=null){//编辑
    		this.master = masterService.get(Integer.parseInt(id));
    	}else{
    		//新增
    		master = new Master();
    	}
    	gridModel = masterService.getAll(Master.class);
    	groupList = groupService.getAll(Group.class);
    	departList = departService.getAll(Depart.class);
    	return "update";
    }
	
	public String save() throws Exception {
		Group g = groupService.get(groupid);
		Depart d = departService.get(departid);
		master.setDepart(d);
		master.setDept(d.getDeptname());
		
		if(master.getId()!= null){//编辑
			Master m = masterService.get(master.getId());
			m.setDepart(master.getDepart());
			m.setDept(master.getDept());
			m.setName(master.getName());
			m.setTruename(master.getTruename());
			masterService.update(m);
			
		}else{//新增
			master.setBdate(new Date());
			master.setPassword(master.getName());
			//获取session中用户
			Master m0 = (Master)ServletActionContext.getRequest().getSession().getAttribute("user");
			master.setMasterid(m0.getId());
			master.setMastername(m0.getName());
			Master m = masterService.save(master);
			
			Mastergroup mg = new Mastergroup();
			mg.setGroup(g);
			mg.setCreatedate(new Date());
			mg.setMaster(master);
			mgService.save(mg);
		}
		return "user";
	}

	public List<Master> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Master> gridModel) {
		this.gridModel = gridModel;
	}

	@Override
	public String refreshGridModel() {
		record = masterService.getTotalCount(Master.class);
		Pages pages = new Pages();		
    	pages.setPage(page);
    	pages.setTotalNum(record);
    	pages.setPerPageNum(rows);
    	pages.executeCount();
    	total = pages.getAllPage();
    	
    	if(!StringUtil.isEmpty(this.sidx) && !StringUtil.isEmpty(this.sord)){
    		Condition condition1 = new Condition();
        	condition1.setName(this.sidx);
    		//condition1.setValue(Condition.DESC);
    		condition1.setValue(this.sord);
    		condition1.setType(ConditionType.ORDER);
    		conditions.add(condition1);
    	}
    	//只实现equal
    	if(this.search && !StringUtil.isEmpty(this.searchField)
    			&& !StringUtil.isEmpty(this.searchOper)&& !StringUtil.isEmpty(this.searchString)){
    		Condition condition2 = new Condition();
    		condition2.setName(this.searchField);
    		//condition1.setValue(Condition.DESC);
    		condition2.setValue(this.searchString);
    		condition2.setType(ConditionType.PROPERTY);
    		condition2.setOperate(ConditionOperate.EQUAL);
    		conditions.add(condition2);
    	}
    		
    	pageList = masterService.findByPages(conditions, pages);
		pages = pageList.getPages();
		
        this.rows = pages.getPerPageNum();
        this.record = pages.getTotalNum();
        this.page = pages.getPage();
        
        gridModel = pageList.getObjectList();
        
/*		for(Ztree z :gridModel){
			z.setGroup(null);
		}*/
		System.out.println("rows:"+rows+" page:"+ page + " total:" + total + " record:" + record);
		return SUCCESS;
	}

	public Master getMaster() {
		return master;
	}

	public void setMaster(Master master) {
		this.master = master;
	}

	public Integer getGroupid() {
		return groupid;
	}

	public void setGroupid(Integer groupid) {
		this.groupid = groupid;
	}

	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}

	public Integer getDepartid() {
		return departid;
	}

	public void setDepartid(Integer departid) {
		this.departid = departid;
	}

	public List<Depart> getDepartList() {
		return departList;
	}

	public void setDepartList(List<Depart> departList) {
		this.departList = departList;
	}

}
	
